package com.neshai.webapp.controller;

import com.neshai.webapp.entity.User;
import com.neshai.webapp.mapper.UserMapper;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping({"/api"})
@RequiredArgsConstructor
public class UserController {

    private final UserMapper userMapper;

    @GetMapping("/user/usage")
    public Map<String, Object> getUser(HttpServletRequest request) {
        Object idObject = request.getAttribute("id");
        if (idObject instanceof String) {
            User user = userMapper.selectById(Integer.valueOf((String) idObject));
            Map<String, Object> map = new HashMap<>();
            map.put("usageBasic", user.getUsageBasic());
            map.put("usagePremium", user.getUsagePremium());
            map.put("vipLevel", user.getVipLevel());
            return map;
        } else {
            return null;
        }
    }

    @GetMapping("/user/info")
    public Map<String, Object> getUserInfo(HttpServletRequest request) {
        Object idObject = request.getAttribute("id");
        if (idObject instanceof String) {
            User user = userMapper.selectById(Integer.valueOf((String) idObject));
            Map<String, Object> map = new HashMap<>();
            map.put("email", user.getEmail());
            map.put("walletAddress", user.getWalletAddress());
            map.put("vipLevel", user.getVipLevel());
            map.put("balance", user.getBalance());
            map.put("expiryTime", user.getExpiryTime());
            map.put("subscriptionStatus",user.getSubscriptionStatus());
            return map;
        } else {
            return null;
        }
    }
}